package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.GlobalConstants;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.system.dto.UserMenuDTO;
import com.ctshk.rpc.system.entity.SysMenu;
import com.ctshk.rpc.system.entity.SysPermission;
import com.ctshk.rpc.system.entity.UserOftenMenu;
import com.ctshk.rpc.system.mapper.SysMenuMapper;
import com.ctshk.rpc.system.mapper.SysPermissionMapper;
import com.ctshk.rpc.system.mapper.UserOftenMenuMapper;
import com.ctshk.rpc.system.req.UserOftenMenuReq;
import com.ctshk.rpc.system.service.IUserOftenMenuService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户常用菜单 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-26
 */
@DubboService
public class UserOftenMenuServiceImpl extends ServiceImpl<UserOftenMenuMapper, UserOftenMenu> implements IUserOftenMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private UserOftenMenuMapper userOftenMenuMapper;

    @Override
    public List<UserMenuDTO> list(TokenUser currentUser) {
        boolean isAdmin = GlobalConstants.Role.isAdmin(currentUser.getUsername());
        Long userId = currentUser.getId();

        // 获取当前账号的菜单数组
        List<SysPermission> permissionList = sysPermissionMapper.selectList(Wrappers.<SysPermission>lambdaQuery()
                .select(SysPermission::getMenuId)
                .eq(!isAdmin, SysPermission::getUserId, userId)
                .eq(SysPermission::getMenuType, 2));
        List<Long> menuIdList = permissionList.stream().map(SysPermission::getMenuId).collect(Collectors.toList());
        List<SysMenu> sysMenuList = sysMenuMapper.selectBatchIds(menuIdList);
        List<UserMenuDTO> userMenuDTOList = new ArrayList<>();
        sysMenuList.forEach(sysMenu -> {
            UserMenuDTO userMenuDTO = new UserMenuDTO();
            userMenuDTO.setMenuId(sysMenu.getId());
            userMenuDTO.setTitle(sysMenu.getTitle());
            userMenuDTOList.add(userMenuDTO);
        });
        return userMenuDTOList;
    }

    @Override
    public Result saveUserOftenMenu(UserOftenMenuReq userOftenMenuReq, Long userId) {
        List<UserOftenMenu> userOftenMenus = new ArrayList<>();
        userOftenMenuReq.getMenuIds().forEach(menuId -> {
            UserOftenMenu userOftenMenu = new UserOftenMenu();
            userOftenMenu.setUserId(userId);
            userOftenMenu.setMenuId(menuId);
            userOftenMenu.setStatus(1);
            userOftenMenu.setCreateId(userId);
            userOftenMenu.setIsDeleted(IsDeletedCode.NO.getCode());
            userOftenMenu.setId(SnowflakeIdWorker.nextId());
            userOftenMenu.setGmtCreate(LocalDateTime.now());
            userOftenMenus.add(userOftenMenu);
        });
        userOftenMenuMapper.insertBatchSomeColumn(userOftenMenus);
        return Result.success();
    }

    @Override
    public List<UserMenuDTO> oftenMenuList(TokenUser currentUser) {
        List<UserOftenMenu> list = userOftenMenuMapper.selectList(Wrappers.<UserOftenMenu>lambdaQuery()
                .select(UserOftenMenu::getMenuId)
                .eq(UserOftenMenu::getUserId, currentUser.getId()));
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<Long> menuIdList = list.stream().map(UserOftenMenu::getMenuId).collect(Collectors.toList());
        List<SysMenu> sysMenuList = sysMenuMapper.selectBatchIds(menuIdList);
        List<UserMenuDTO> userMenuDTOList = new ArrayList<>();
        sysMenuList.forEach(sysMenu -> {
            UserMenuDTO userMenuDTO = new UserMenuDTO();
            userMenuDTO.setMenuId(sysMenu.getId());
            userMenuDTO.setTitle(sysMenu.getTitle());
            userMenuDTOList.add(userMenuDTO);
        });
        return userMenuDTOList;
    }
}
